<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click.native="showDialog=true">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column align="center" width="100" label="序号" prop="id" />
        <el-table-column align="center" width="200" label="角色">
          <template #default="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="启用">
          <template v-slot="{ row }">
            <el-switch v-if="row.isEdit" v-model="row.editRow.state" :active-value="1" :inactive-value="0" />
            <span v-else>{{ row.state === 1 ? '已启用' : row.state ===0? "未启用":'无' }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述" prop="description">
          <template v-slot="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <template v-if="row.isEdit">
              <el-button type="primary" size="mini" @click="onEdit(row)">确定</el-button>
              <el-button size="mini" @click="cancelEdit(row)">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text" size="mini">分配权限 </el-button>
              <el-button type="text" size="mini" @click="edit(row)">编辑 </el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" type="text" size="mini" style="margin-left: 10px;">删除</el-button>
              </el-popconfirm>
            </template>

          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          :page-size="params.pagesize"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog" @close="onCancel">
      <!-- 表单内容 -->
      <el-form ref="formref" label-width="120px" :model="roleForm" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" style="width:300px" size="mini" />
        </el-form-item>

        <el-form-item label="启用" prop="state">
          <el-switch v-model="roleForm.state" :active-value="1" :inactive-value="0" size="mini" />
        </el-form-item>

        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>

        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="onSubmit">确定</el-button>
              <el-button size="mini" @click="onCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import { addRole, delRole, getRoleListApi, updateRole } from '@/api/role'

export default {
  name: 'Role',
  data() {
    return {
      params: {
        page: 1,
        pagesize: 5
      },
      total: 0,
      list: [],
      showDialog: false,
      roleForm: {
        name: '',
        description: '',
        state: 0 // 默认未1启用 关闭 0 打开1
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async edit(row) {
      row.isEdit = true
      // 重新给临时行数据对象赋值
      row.editRow.name = row.name
      row.editRow.description = row.description
      row.editRow.state = row.state
    },
    async onEdit(row) {
      if (!row.editRow.name.trim() || !row.editRow.description.trim()) {
        this.$message.warning('角色名称和描述不能为空~')
        return
      }
      // 2. 调用接口

      await updateRole({ id: row.id, ...row.editRow })
      this.$message.success('修改角色成功~')
      // 3. 退出编辑
      // row.isEdit = false
      Object.assign(row, {
        isEdit: false,
        ...row.editRow
      })
    },
    cancelEdit(row) {
      row.isEdit = false
    },
    async  getRoleList() {
      const res = await getRoleListApi(this.params)
      this.list = res.rows
      this.total = res.total
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    },
    changePage(value) {
      this.params.page = value
      this.getRoleList()
    },
    async onSubmit() {
      await this.$refs.formref.validate()
      await addRole(this.roleForm)
      this.$message.success('新增成功')
      this.getRoleList()
      this.onCancel()
    },
    async onCancel() {
      await this.$refs.formref.resetFields()
      this.showDialog = false
    },
    async confirmDel(id) {
      await delRole(id)
      this.$message.success('删除成功')
      if (this.list.length === 1) this.params.page--
      this.getRoleList()
    }
  }
}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
